package com.intelligent.system.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.system.auth.dto.OrganizationDTO;
import com.intelligent.system.auth.dto.OrganizationQueryDTO;
import com.intelligent.system.auth.entity.Organization;
import com.intelligent.system.auth.service.OrganizationService;
import com.intelligent.system.common.response.ResultDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织机构控制器
 */
@Api(tags = "组织机构管理接口")
@RestController
@RequestMapping("/api/organizations")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation("根据ID查询组织机构")
    @GetMapping("/{id}")
    public ResultDO<OrganizationDTO> getById(
            @ApiParam(value = "组织机构ID", required = true)
            @PathVariable Long id) {
        return organizationService.getOrganizationById(id);
    }

    @ApiOperation("查询所有组织机构")
    @GetMapping("/all")
    public ResultDO<List<OrganizationDTO>> listAll() {
        return organizationService.listAll();
    }

    @ApiOperation("根据父级ID查询组织机构列表")
    @GetMapping("/parent/{parentId}")
    public ResultDO<List<OrganizationDTO>> listByParentId(
            @ApiParam(value = "父级组织机构ID", required = true)
            @PathVariable Long parentId) {
        return organizationService.listByParentId(parentId);
    }

    @ApiOperation("分页查询组织机构列表")
    @PostMapping("/page")
    public ResultDO<IPage<OrganizationDTO>> page(
            @ApiParam(value = "查询参数", required = true)
            @Validated @RequestBody OrganizationQueryDTO queryDTO) {
        return organizationService.page(queryDTO);
    }

    @ApiOperation("新增组织机构")
    @PostMapping("/add")
    public ResultDO<?> save(
            @ApiParam(value = "组织机构信息", required = true)
            @Validated @RequestBody OrganizationDTO organizationDTO) {
        return organizationService.save(organizationDTO);
    }

    @ApiOperation("更新组织机构")
    @PostMapping("/update")
    public ResultDO<?> update(
            @ApiParam(value = "组织机构信息", required = true)
            @Validated @RequestBody OrganizationDTO organizationDTO) {
        return organizationService.update(organizationDTO);
    }

    @ApiOperation("删除组织机构")
    @DeleteMapping("/{id}")
    public ResultDO<?> deleteById(
            @ApiParam(value = "组织机构ID", required = true)
            @PathVariable Long id) {
        return organizationService.deleteById(id);
    }

    @ApiOperation("根据组织机构编码查询组织机构")
    @GetMapping("/code/{orgCode}")
    public ResultDO<OrganizationDTO> getByOrgCode(
            @ApiParam(value = "组织机构编码", required = true)
            @PathVariable String orgCode) {
        return organizationService.getByOrgCode(orgCode);
    }

    @ApiOperation("获取组织机构及其联系人信息")
    @GetMapping("/{id}/contacts")
    public ResultDO<OrganizationDTO> getOrganizationWithContacts(
            @ApiParam(value = "组织机构ID", required = true)
            @PathVariable Long id) {
        return organizationService.getOrganizationWithContacts(id);
    }
} 